﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using App.Data;
using App.CustomEntity;
using App.Core.Extensions;

/// <summary>
/// Summary description for AjaxService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class AjaxService : System.Web.Services.WebService {

    public AjaxService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    /// <summary>
    /// Gets Zones By CityID
    /// </summary>
    /// <param name="cityID"></param>
    /// <returns></returns>
    [WebMethod]
    public List<CustomZone> GetZonesOfCity(int cityID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var zones = from P in context.Zones where P.CityID == cityID orderby P.Name
                    select new CustomZone { Name = P.Name, ID = P.ID };
        return zones == null ? null : zones.ToList();
    }
    [WebMethod]
    public String GetUnitNameForPropertyType(int propertyTypeID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var unit = from P in context.UnitOfPropertyTypes where P.PropertyTypeID == propertyTypeID select P.Unit;
        if (unit != null && unit.Count() > 0)
            return unit.ToList()[0].Name;
        
        return String.Empty;
    }
    [WebMethod]
    public List<App.CustomEntity.BusinessType> GetChildBusinessTypes(int parentBusinessTypeID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var types = from P in context.BusinessTypes where P.ParentID == parentBusinessTypeID 
                        select new App.CustomEntity.BusinessType 
                        { 
                            ID=P.ID,
                            LongName = P.LongName,
                            ShortName = P.ShortName
                        };
        return types == null || types.Count() == 0 ? null : types.ToList();
    }
    /// <summary>
    /// Gets Cities by Country
    /// </summary>
    /// <param name="countryID"></param>
    /// <returns></returns>
    [WebMethod]
    public List<App.CustomEntity.City> GetCitiesByCountry(int countryID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var cities = from P in context.Cities 
                     where P.CountryID == countryID orderby P.Name
                     select new App.CustomEntity.City { ID = P.ID, CountryID = P.CountryID, Name = P.Name } ;
        return cities == null || cities.Count() == 0 ? null : cities.ToList();
    }
    /// <summary>
    /// Subscribes an Email for BTC Real Estate News Letter Service
    /// </summary>
    /// <param name="email"></param>
    /// <returns></returns>
    [WebMethod]
    public int SubscribeNewsLetter(String email)
    {
        if (email.IsNullOrEmpty())
            throw new ArgumentException("Email cannot be null or empty.");

        BtcHousingEntities context = new BtcHousingEntities();
        var existingSubscriber = context.NewsletterSubscribers.FirstOrDefault(P => String.Compare(P.Email, email, true) == 0);
        if (existingSubscriber == null)
        {
            NewsletterSubscriber subscriber = new NewsletterSubscriber();
            subscriber.Email = email;
            subscriber.Created = DateTime.Now;
            subscriber.Modified = DateTime.Now;
            subscriber.IsActive = true;
            subscriber.VerificationKey = System.Guid.NewGuid();

            context.AddToNewsletterSubscribers(subscriber);
            context.SaveChanges();
            return 1;
        }
        else
            return 2; ///Already Exists        
    }
}
